package com.tianyu.inspection.service;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tianyu.inspection.common.Constants;
import com.tianyu.inspection.common.enums.DangerStatus;
import com.tianyu.inspection.common.enums.Role;
import com.tianyu.inspection.common.model.PageModel;
import com.tianyu.inspection.entity.*;
import com.tianyu.inspection.service.api.IDangerProcessService;
import com.tianyu.inspection.service.api.IDangerService;
import com.tianyu.inspection.service.websocket.WebSocketService;
import com.tianyu.inspection.common.component.AppConstants;
import com.tianyu.inspection.common.component.MapperModel;
import com.tianyu.inspection.entity.model.DangerQueryMo;
import com.tianyu.inspection.entity.model.OperateMo;
import com.tianyu.inspection.entity.model.QueryMo;
import com.tianyu.inspection.entity.vo.ServiceRo;
import com.tianyu.inspection.mapper.DangerMapper;
import com.tianyu.inspection.mapper.DangerProcessMapper;
import com.tianyu.inspection.mapper.DangerTypeMapper;
import com.tianyu.inspection.mapper.UserMapper;
import io.ocs.model.OcsSimpleModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.ui.ModelMap;

import javax.annotation.Resource;
import java.sql.SQLIntegrityConstraintViolationException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**********************************
 * @Author YSW
 * @Description 险情服务
 * @Date 2021/1/12 - 11:54
 **********************************/

@Slf4j
@Service
public class DangerServiceImpl implements IDangerService {

    @Resource
    private IDangerProcessService dangerProcessService;

    @Resource
    private DangerMapper dangerMapper;

    @Resource
    private DangerProcessMapper dangerProcessMapper;

    @Resource
    private DangerTypeMapper dangerTypeMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private AppConstants appConstants;

    @Resource
    private WebSocketService webSocketService;

    @Resource
    private MapperModel mapperModel;

    @Resource
    private OcsSimpleModel ocsSimpleModel;

    /**
     * 查询单个险情的对应处理进程
     *
     * @param queryMo 查询参数
     * @return IPage<ModelMap>
     */
    @Override
    public ModelMap dangerStatistics(QueryMo queryMo) {
        ModelMap modelMap = new ModelMap();
        ModelMap eqMap = queryMo.getEqMap();
        ModelMap stateMap = new ModelMap();
        eqMap.put("state", 0);
        QueryWrapper<Danger> wrapper = new QueryWrapper<>();
        if (Objects.nonNull(queryMo.get("startTime"))) {
            wrapper.ge("create_time", queryMo.get("startTime").toString());
        }
        if (Objects.nonNull(queryMo.get("endTime"))) {
            wrapper.le("create_time", queryMo.get("endTime").toString());
        }
        Integer total = Math.toIntExact(dangerMapper.selectCount(wrapper.clone().allEq(eqMap)));
        eqMap.put("status", 0);
        Integer newDanger = Math.toIntExact(dangerMapper.selectCount(wrapper.clone().allEq(eqMap)));
        eqMap.put("status", 1);
        Integer exeDanger = Math.toIntExact(dangerMapper.selectCount(wrapper.clone().allEq(eqMap)));
        eqMap.put("status", 2);
        Integer finishDanger = Math.toIntExact(dangerMapper.selectCount(wrapper.clone().allEq(eqMap)));
        stateMap.put("未指派", newDanger);
        stateMap.put("处理中", exeDanger);
        stateMap.put("已解决", finishDanger);
        modelMap.put("total", total);
        modelMap.put("state", stateMap);
        List<DangerType> dangerTypes = dangerTypeMapper.selectList(
                new QueryWrapper<>());
        ModelMap typeMap = new ModelMap();
        eqMap.remove("status");
        dangerTypes.forEach(dangerType -> {
            eqMap.put("danger_type_id", dangerType.getId());
            Integer typeCount = Math.toIntExact(dangerMapper.selectCount(wrapper.clone().allEq(eqMap)));
            typeMap.put(dangerType.getTypeName(), typeCount);
        });
        modelMap.put("type", typeMap);
        return modelMap;
    }

    /**
     * 巡线员分页查询险情并获取对应的险情类型
     *
     * @param queryMo 查询参数
     * @return
     */
    @Override
    public IPage<ModelMap> selectPatrolDangerAndDangerTypeList(QueryMo queryMo) {
        Page<ModelMap> page = new Page<>(queryMo.getPageNum(), queryMo.getPageSize());
        ObjectMapper objectMapper = new ObjectMapper();
        DangerQueryMo dangerQueryMo;
        try {
            dangerQueryMo = objectMapper.convertValue(queryMo, DangerQueryMo.class);
        } catch (Exception e) {
            e.printStackTrace();
            return page;
        }
        if (queryMo.getPageSize() != -1) {
            if (queryMo.getPageNum() <= 0) {
                queryMo.getEqMap().put("pageNum", 1);
            }
            int startIndex = (queryMo.getPageNum() - 1) * queryMo.getPageSize();
            dangerQueryMo.setStartIndex(startIndex);
            dangerQueryMo.setEndIndex(startIndex + queryMo.getPageSize());
        }
        dangerQueryMo.setAscParams(queryMo.getAscParams());
        dangerQueryMo.setDescParams(queryMo.getDescParams());
        List<ModelMap> modelMaps = dangerMapper.selectPatrolDangerAndDangerTypeList(dangerQueryMo);
        modelMaps.forEach(modelMap -> {
            Object sceneImage = modelMap.get("sceneImage");
            Object labelImage = modelMap.get("labelImage");
            Object icon = modelMap.get("typeIcon");
            Object levelIcon = modelMap.get("levelIcon");
            Object levelIconClick = modelMap.get("levelIconClick");
            if (Objects.nonNull(sceneImage)) {
                modelMap.put("sceneImage", ocsSimpleModel.getObjectUrl((String) sceneImage));
            }
            if (Objects.nonNull(labelImage)) {
                modelMap.put("labelImage", ocsSimpleModel.getObjectUrl((String) labelImage));
            }
            if (Objects.nonNull(icon)) {
                modelMap.put("typeIcon", ocsSimpleModel.getObjectUrl((String) icon));
            }
            if (Objects.nonNull(levelIcon)) {
                modelMap.put("levelIcon", ocsSimpleModel.getObjectUrl((String) levelIcon));
            }
            if (Objects.nonNull(levelIconClick)) {
                modelMap.put("levelIconClick", ocsSimpleModel.getObjectUrl((String) levelIconClick));
            }
        });
        page.setRecords(modelMaps);
        QueryWrapper<Danger> wrapper = new QueryWrapper<Danger>().allEq(queryMo.getEqMap());
        if (Objects.nonNull(dangerQueryMo.getDangerIds()) && dangerQueryMo.getDangerIds().size() != 0) {
            wrapper.in("id", dangerQueryMo.getDangerIds());
        }
        if (Objects.nonNull(dangerQueryMo.getMultiStatus()) && dangerQueryMo.getMultiStatus().size() != 0) {
            wrapper.in("status", dangerQueryMo.getMultiStatus());
        }
        page.setTotal(dangerMapper.selectPatrolDangerCount(dangerQueryMo));
        page.setCurrent(queryMo.getPageNum());
        return page;
    }

    /**
     * 巡线员分页查询险情并获取对应的险情类型
     *
     * @param queryMo 查询参数
     * @return
     */
    @Override
    public IPage<ModelMap> selectPatrolDangerAndDangerTypeList2(QueryMo queryMo) {
        Page<ModelMap> page = new Page<>(queryMo.getPageNum(), queryMo.getPageSize());
        ObjectMapper objectMapper = new ObjectMapper();
        DangerQueryMo dangerQueryMo;
        try {
            dangerQueryMo = objectMapper.convertValue(queryMo, DangerQueryMo.class);
        } catch (Exception e) {
            e.printStackTrace();
            return page;
        }
        if (queryMo.getPageSize() != -1) {
            if (queryMo.getPageNum() <= 0) {
                queryMo.getEqMap().put("pageNum", 1);
            }
            int startIndex = (queryMo.getPageNum() - 1) * queryMo.getPageSize();
            dangerQueryMo.setStartIndex(startIndex);
            dangerQueryMo.setEndIndex(startIndex + queryMo.getPageSize());
        }
        dangerQueryMo.setAscParams(queryMo.getAscParams());
        dangerQueryMo.setDescParams(queryMo.getDescParams());
        IPage<JSONObject> jsonObjectIPage = dangerMapper.selectPatrolDangerAndDangerTypeList2(page, dangerQueryMo.getProjectId(),
                dangerQueryMo.getInspectorId(),
                dangerQueryMo.getDangerTypeId(),
                dangerQueryMo.getStatus(),
                dangerQueryMo.getMultiStatus());
        jsonObjectIPage.getRecords().forEach(jsonObject -> {
            String sceneImage = jsonObject.getString("sceneImage");
            String labelImage = jsonObject.getString("labelImage");
            String icon = jsonObject.getString("typeIcon");
            String levelIcon = jsonObject.getString("levelIcon");
            String levelIconClick = jsonObject.getString("levelIconClick");
            if (Objects.nonNull(sceneImage)) {
                jsonObject.put("sceneImage", ocsSimpleModel.getObjectUrl(sceneImage));
            }
            if (Objects.nonNull(labelImage)) {
                jsonObject.put("labelImage", ocsSimpleModel.getObjectUrl(labelImage));
            }
            if (Objects.nonNull(icon)) {
                jsonObject.put("typeIcon", ocsSimpleModel.getObjectUrl(icon));
            }
            if (Objects.nonNull(levelIcon)) {
                jsonObject.put("levelIcon", ocsSimpleModel.getObjectUrl(levelIcon));
            }
            if (Objects.nonNull(levelIconClick)) {
                jsonObject.put("levelIconClick", ocsSimpleModel.getObjectUrl(levelIconClick));
            }
        });
        return page;
    }

    /**
     * 分页查询险情详情  携带险情类型、等级、处理情况等信息
     *
     * @param queryMo 查询参数
     * @return IPage<ModelMap>
     */
    @Override
    public IPage<Danger> selectDangerDeepDetailsList(QueryMo queryMo) {
        final IPage<Danger> page = selectDangerDetailsList(queryMo);
        final Map<Integer, User> userMap = mapperModel.getUserMap(queryMo.getEqMap().get("project_id"), 4);
        page.getRecords().forEach(danger -> {
            List<DangerProcess> dangerProcessList = dangerProcessMapper.selectList(new QueryWrapper<DangerProcess>()
                    .select("id", "description", "scene_image", "deal_time", "status", "inspector_id")
                    .eq("state", 0)
                    .eq("project_id", queryMo.getEqMap().get("project_id"))
                    .eq("danger_id", danger.getId()));
            dangerProcessList.forEach(dangerProcess -> {
                final Integer inspectorId = dangerProcess.getInspectorId();
                if (dangerProcess.getSceneImage() != null) {
                    dangerProcess.setSceneImage(ocsSimpleModel.getObjectUrl(dangerProcess.getSceneImage()));
                }
                dangerProcess.setInspectorName(userMap.get(inspectorId).getNickName());
                dangerProcess.setInspectorPhone(userMap.get(inspectorId).getPhone());

            });
            danger.setDangerProcessList(dangerProcessList);
        });
        return page;
    }

    /**
     * 分页查询险情详情  携带险情类型、等级
     *
     * @param queryMo 查询参数
     * @return IPage<ModelMap>
     */
    @Override
    public IPage<Danger> selectDangerDetailsList(QueryMo queryMo) {
        if (Objects.isNull(queryMo.getAscParams()) && Objects.isNull(queryMo.getDescParams())) {
            queryMo.setDescParams("create_time");
        }
        QueryWrapper<Danger> wrapper = new QueryWrapper<>();
        final Long startTime = queryMo.getLong("startTime");
        final Long endTime = queryMo.getLong("endTime");
        if (startTime != null && endTime != null) {
            String format = "'%Y-%m-%d %H:%i:%S'";
            String formatPattern = "yyyy-MM-dd HH:mm:ss";
            SimpleDateFormat sdf = new SimpleDateFormat(formatPattern);
            String startTimeFormat = sdf.format(new Date(startTime));
            String endTimeFormat = sdf.format(new Date(endTime));
            wrapper.between("DATE_FORMAT(create_time, " + format + ")", startTimeFormat, endTimeFormat);
        }
        IPage<Danger> page = PageModel.getModelPage(wrapper,queryMo, dangerMapper);
        final Map<Integer, DangerType> dangerTypeMap = mapperModel.getDangerTypeMap();
        final Map<Integer, DangerLevel> dangerLevelMap = mapperModel.getDangerLevelMap();
        page.getRecords().forEach(danger -> {
            danger.setTypeName(dangerTypeMap.get(danger.getDangerTypeId()).getTypeName());
            if (Objects.nonNull(danger.getLabelImage())) {
                danger.setLabelImage(ocsSimpleModel.getObjectUrl(danger.getLabelImage()));
            }
            danger.setTypeIcon(ocsSimpleModel.getObjectUrl(dangerTypeMap.get(danger.getDangerTypeId()).getIcon()));
            danger.setLevelName(dangerLevelMap.get(danger.getDangerLevelId()).getLevelName());
            danger.setLevelIcon(ocsSimpleModel.getObjectUrl(dangerLevelMap.get(danger.getDangerLevelId() + 1).getIcon()));
            danger.setLevelIconClick(ocsSimpleModel.getObjectUrl(dangerLevelMap.get(danger.getDangerLevelId() + 1).getIconClick()));
        });
        return page;
    }

    /**
     * 插入一条数据
     *
     * @param operateMo 创建实体对象所需参数
     * @return ModelMap
     */
    @Override
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED)
    public ServiceRo insertDangerAndDangerProcess(OperateMo operateMo, Integer labelUserId) {
        ObjectMapper objectMapper = new ObjectMapper();
        Danger danger = objectMapper.convertValue(operateMo.getEntity(), Danger.class);
        List<Integer> inspectors = danger.getInspectors();
        Integer projectId = danger.getProjectId();
        if (Objects.isNull(projectId)) {
            return new ServiceRo().setResult(false).setMessage("超级管理员用户参数[projectId]不能为空");
        }
        danger.setStatus(DangerStatus.DANGER_STATUS_NOT_ASSIGN.getValue());
        danger.setCreateTime(new Date());
        try {
            if (dangerMapper.insert(danger) == 1) {
                log.info("用户[" + labelUserId + "] 标记险情 [" + danger.getId() + "]");
                webSocketService.sendDangerMessage(danger);
                if (Objects.nonNull(inspectors)) {
                    ArrayList<DangerProcess> dangerProcessList = new ArrayList<>();
                    inspectors.forEach(userId -> {
                        //巡线员ID参数检查
                        if (Constants.NUMBER_PATTERN.matcher(userId.toString()).matches()) {
                            //巡线员是否存在
                            if (1 == userMapper.selectCount(new QueryWrapper<User>()
                                    .eq("id", userId).eq("project_id", projectId).eq("role_id", Role.ROLE_PATROL.getValue()))) {
                                DangerProcess dangerProcess = new DangerProcess();
                                dangerProcess.setProjectId(projectId);
                                dangerProcess.setInspectorId(userId);
                                dangerProcess.setDangerId(danger.getId());
                                dangerProcessList.add(dangerProcess);
                            }
                        }
                    });
                    int batchInsert = dangerProcessMapper.batchInsert(dangerProcessList);
                    if (inspectors.size() == batchInsert) {
                        Danger danger0 = new Danger();
                        danger0.setId(danger.getId());
                        danger0.setStatus(DangerStatus.DANGER_STATUS_PROCESSING.getValue());
                        dangerMapper.updateById(danger0);
                        danger.setInspectors(null);
                        danger.setStatus(null);
                        danger.setLabelImage(ocsSimpleModel.getObjectUrl(danger.getLabelImage()));
                        for (DangerProcess dangerProcess0 : dangerProcessList) {
                            final ModelMap patrolDanger = dangerMapper.selectPatrolDangerAndDangerTypeOne(dangerProcess0);
                            patrolDanger.put("inspectorId", dangerProcess0.getInspectorId());
                            Object sceneImage = patrolDanger.get("sceneImage");
                            Object labelImage = patrolDanger.get("labelImage");
                            Object icon = patrolDanger.get("typeIcon");
                            Object levelIcon = patrolDanger.get("levelIcon");
                            Object levelIconClick = patrolDanger.get("levelIconClick");
                            if (Objects.nonNull(sceneImage)) {
                                patrolDanger.put("sceneImage", ocsSimpleModel.getObjectUrl((String) sceneImage));
                            }
                            if (Objects.nonNull(labelImage)) {
                                patrolDanger.put("labelImage", ocsSimpleModel.getObjectUrl((String) labelImage));
                            }
                            if (Objects.nonNull(icon)) {
                                patrolDanger.put("typeIcon", ocsSimpleModel.getObjectUrl((String) icon));
                            }
                            if (Objects.nonNull(levelIcon)) {
                                patrolDanger.put("levelIcon", ocsSimpleModel.getObjectUrl((String) levelIcon));
                            }
                            if (Objects.nonNull(levelIconClick)) {
                                patrolDanger.put("levelIconClick", ocsSimpleModel.getObjectUrl((String) levelIconClick));
                            }
                            webSocketService.sendPatrolDangerMessage(patrolDanger);
                        }
                        log.debug("插入险情处理记录成功 [{}] 条！", inspectors.size());
                        return new ServiceRo();
                    } else {
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        log.error("数据库操作错误[险情处理插入未全部成功], 数据操作已回滚");
                        return new ServiceRo().setResult(false).setMessage("险情处理插入未全部成功, 插入险情处理失败");
                    }
                }
                return new ServiceRo();
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            log.error("数据库操作错误, 数据操作已回滚,错误信息[{}]", e.getCause().getMessage());
            return new ServiceRo().setResult(false).setMessage("险情添加失败");
        }
        return new ServiceRo().setResult(false).setMessage("险情添加失败");
    }

    /**
     * 分页查询
     *
     * @param queryMo 参数模型
     * @return ModelMap
     */
    @Override
    public IPage<Danger> searchListPage(QueryMo queryMo) {
        IPage<Danger> page = PageModel.getModelPage(queryMo, dangerMapper);
        page.getRecords().forEach(danger -> {
            if (Objects.nonNull(danger.getLabelImage())) {
                danger.setLabelImage(ocsSimpleModel.getObjectUrl(danger.getLabelImage()));
            }
        });
        return page;
    }

    /**
     * 通过ID查询一条数据
     *
     * @param queryMo 参数集[实体ID 与 项目ID, 数据状态等]
     * @return
     */
    @Override
    public Danger selectOne(QueryMo queryMo) {
        if (Objects.isNull(queryMo.get("id"))) {
            return null;
        }
        final IPage<Danger> page = selectDangerDeepDetailsList(queryMo);
        if (page.getTotal() == 1) {
            return page.getRecords().get(0);
        }
        return null;
    }

    /**
     * 插入一条数据
     *
     * @param operateMo 实体对象
     * @return 是否操作成功
     */
    @Override
    public boolean insert(OperateMo operateMo) {
        ObjectMapper objectMapper = new ObjectMapper();
        Danger danger = objectMapper.convertValue(operateMo.getEntity(), Danger.class);
        boolean result = 1 == dangerMapper.insert(danger);
        if (result) {
            webSocketService.sendDangerMessage(danger);
        }
        return result;
    }

    /**
     * 通过ID删除一条数据
     *
     * @param queryMo 参数集[实体ID 与 项目ID, 数据状态等]
     * @return 是否操作成功
     */
    @Override
    public boolean delete(QueryMo queryMo) {
        if (Objects.nonNull(queryMo.getEqMap().get("id"))) {
            return 1 == dangerMapper.delete(new QueryWrapper<Danger>().allEq(queryMo.getEqMap()));
        }
        return false;
    }

    /**
     * 通过ID更新一条数据
     *
     * @param operateMo 实体对象参数集
     * @return
     */
    @Override
    public boolean update(OperateMo operateMo) {
        if (1 == dangerMapper.selectCount(new QueryWrapper<Danger>().allEq(operateMo.getEqMap()))) {
            ObjectMapper objectMapper = new ObjectMapper();
            Danger danger = objectMapper.convertValue(operateMo.getEntity(), Danger.class);
            return 1 == dangerMapper.update(danger, new QueryWrapper<Danger>().allEq(operateMo.getEqMap()));
        } else {
            return false;
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED)
    public ServiceRo updateDangerAndDangerProcessUser(OperateMo operateMo) {
        ObjectMapper objectMapper = new ObjectMapper();
        operateMo.getEntity().put("id", operateMo.get("id"));
        operateMo.getEntity().put("projectId", operateMo.get("projectId"));
        Danger danger = objectMapper.convertValue(operateMo.getEntity(), Danger.class);
        Integer dangerId = danger.getId();
        if (danger.getProjectId() == null) {
            Danger selectDanger = dangerMapper.selectById(dangerId);
            if (Objects.isNull(selectDanger)) {
                return new ServiceRo().setResult(false);
            }
            danger.setProjectId(selectDanger.getProjectId());
        }
        Integer projectId = danger.getProjectId();
        List<Integer> inspectors = danger.getInspectors();
        if (Objects.nonNull(inspectors) && Objects.nonNull(dangerId)) {
            List<DangerProcess> dangerProcessList = dangerProcessMapper.selectList(new QueryWrapper<DangerProcess>().eq("danger_id", dangerId));
            List<Integer> dbInspectorIdList = new ArrayList<>(dangerProcessList.size());
            dangerProcessList.forEach(dangerProcess -> dbInspectorIdList.add(dangerProcess.getInspectorId()));
            List<Integer> insertIdList = new ArrayList<>(dangerProcessList.size());
            List<Integer> deleteIdList = new ArrayList<>(dangerProcessList.size());
            for (Integer inspectorId : inspectors) {
                if (!dbInspectorIdList.contains(inspectorId)) {
                    insertIdList.add(inspectorId);
                }
            }
            dbInspectorIdList.forEach(id -> {
                if (!inspectors.contains(id)) {
                    deleteIdList.add(id);
                }
            });
            if (deleteIdList.size() != 0) {
                AtomicInteger deleteCount = new AtomicInteger();
                deleteIdList.forEach(inspectorId -> {
                    if (dangerProcessMapper.delete(
                            new QueryWrapper<DangerProcess>().eq("project_id", projectId)
                                    .eq("danger_id", dangerId).eq("inspector_id", inspectorId)) == 1) {
                        deleteCount.getAndIncrement();
                    }
                });
                if (deleteCount.get() > 0) {
                    log.debug("删除险情处理记录成功 [{}] 条！", deleteCount.get());
                }
            }
            if (insertIdList.size() != 0) {
                List<DangerProcess> insertDangerProcessList = new ArrayList<>();
                for (Integer userId : insertIdList) {
                    if (1 == userMapper.selectCount(new QueryWrapper<User>()
                            .eq("id", userId).eq("project_id", projectId))) {
                        DangerProcess dangerProcess = new DangerProcess();
                        dangerProcess.setProjectId(projectId).setInspectorId(userId).setDangerId(dangerId);
                        insertDangerProcessList.add(dangerProcess);
                    } else {
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        return new ServiceRo().setResult(false).setMessage("巡线员ID不存在");
                    }
                }
                int batchInsert = dangerProcessMapper.batchInsert(insertDangerProcessList);
                if (insertDangerProcessList.size() == batchInsert) {
                    log.debug("插入险情处理记录成功 [{}] 条！", insertDangerProcessList.size());
                }
            }
        }
        try {
            if (1 == dangerMapper.selectCount(new QueryWrapper<Danger>().allEq(operateMo.getEqMap()))) {
                if (1 == dangerMapper.update(danger, new QueryWrapper<Danger>().allEq(operateMo.getEqMap()))) {
                    return new ServiceRo();
                }
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            log.error("数据库操作错误, 数据操作已回滚,错误信息[{}]", e.getCause().getMessage());
            ServiceRo serviceRo = new ServiceRo().setResult(false);
            if (e.getCause() instanceof SQLIntegrityConstraintViolationException) {
                return serviceRo.setMessage("参数错误 [dangerTypeId | flyTaskId]");
            }
        }
        return new ServiceRo().setResult(false);
    }

    /**
     * 通过ID逻辑删除一条数据
     *
     * @param queryMo 参数集[实体ID 与 项目ID, 数据状态]
     * @return 是否操作成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED)
    public boolean logicallyDelete(QueryMo queryMo) {
        if (1 == dangerMapper.update(
                new Danger().setState(1),
                new QueryWrapper<Danger>().allEq(queryMo.getEqMap())
        )) {
            final QueryMo queryMo1 = new QueryMo();
            queryMo1.put("dangerId", queryMo.get("id"));
            queryMo1.build(DangerProcess.class, (Integer) queryMo.get("projectId"));
            dangerProcessService.logicallyDelete(queryMo1);
        }
        return true;
    }
}